package com.linkzd.iot.utils.mqtt.client;

import com.linkzd.common.utils.DateUtils;
import com.linkzd.iot.domain.DeviceDataLog;
import com.linkzd.iot.domain.DeviceInfo;
import com.linkzd.iot.domain.DeviceFeedbackModel;
import com.linkzd.iot.domain.SensorsReportDataLog;
import com.linkzd.iot.service.IDeviceDataLogService;
import com.linkzd.iot.service.IDeviceFeedbackService;
import com.linkzd.iot.service.IDeviceInfoService;
import com.linkzd.iot.service.ISensorsReportDataLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @author: Wangym
 * @Date: 2024/5/20 20:22
 * @Description:设备数据上报解析
 */
@Component
public class ParsingData {
    int count = 0;
    private static final Logger log = LoggerFactory.getLogger(ParsingData.class);

    @Autowired
    private IDeviceInfoService deviceInfoService;
    @Autowired
    private IDeviceDataLogService deviceDataLogService;
    @Autowired
    private IDeviceFeedbackService deviceFeedbackService;
    @Autowired
    private ISensorsReportDataLogService sensorsReportDataLogService;

    //执行器设备反馈上报解析
    public void feedback(String[] data){
        if(data.length != 2){
            log.info("设备反馈成功消息格式不正确!");
        }else{
            String code = data[0];
            DeviceFeedbackModel deviceFeedbackModel = new DeviceFeedbackModel();
            deviceFeedbackModel.setDeviceCode(code);
            deviceFeedbackModel.setState(0);
            deviceFeedbackModel.setCreateTime(new Date());
            deviceFeedbackService.insertDeviceFeedback(deviceFeedbackModel);
            log.info("设备反馈成功已存入!");
        }
    }


    //传感器设备数据上报解析
    public void analyze(String[] data){
        String code = data[0];
        //类型
        String type = code.substring(3,5);
        //位置
        String position = code.substring(5);
        String msg = null;
        int state = 1;
        String msgData = null;
        if("14".equals(type)){ //温度湿度传感器：(id,温度,湿度)
            Double temperature = Double.parseDouble(data[1]);//温度
            Double humidness = Double.parseDouble(data[2]);//湿度
            msg = "解析数据温度湿度传感器{设备编号:" + code + ",设备类型:" + type + ",设备位置:" + position
                    + ",温度值:" + temperature + ",湿度值:" + humidness + "}";
            msgData = "温度值:" + temperature + ",湿度值:" + humidness;
            if(!(temperature > 0) || !(humidness > 0)){
                state = 0;
            }
        }else if("15".equals(type)){//光照传感器（id,光照值）
            Double illumination = Double.parseDouble(data[1]);//光照值
            illumination = illumination / 100;
            msg = "解析数据光照传感器{设备编号:" + code + ",设备类型:" + type + ",设备位置:" + position
                    + ",光照值:" + illumination + "}";
            msgData = "光照值:" + illumination;
            if(!(illumination > 0)){
                state = 0;
            }
        }else if("16".equals(type)){//土壤湿度传感器（id,土壤湿度值）
            Double soilMoisture = Double.parseDouble(data[1]);//土壤湿度值
            soilMoisture = soilMoisture / 100;
            msg = "解析数据土壤湿度传感器{设备编号:" + code + ",设备类型:" + type + ",设备位置:" + position
                    + ",土壤湿度:" + soilMoisture + "}";
            msgData = "土壤湿度:" + soilMoisture;
            if(!(soilMoisture > 0)){
                state = 0;
            }
        }else if("17".equals(type)){//雨滴传感器（id,雨滴值）
            Double raindrop = Double.parseDouble(data[1]);//雨滴值
            raindrop = raindrop / 100;
            msg = "解析数据雨滴传感器{设备编号:" + code + ",设备类型:" + type + ",设备位置:" + position
                    + ",雨滴值:" + raindrop + "}";
            msgData = "雨滴值:" + raindrop;
            if(!(raindrop > 0)){
                state = 0;
            }
        }else{
            msg = "设备上报类型错误，没有应用的类型!";
        }
        log.info(msg);
        this.saveData(code,msgData,state);
    }

    private void saveData(String code,String msg,int state){
        DeviceInfo device = deviceInfoService.selectDeviceInfoByDeviceCode(code);
        if(!ObjectUtils.isEmpty(device)){
            DeviceInfo deviceInfo = new DeviceInfo();
            deviceInfo.setDeviceCode(code);
            deviceInfo.setDeviceData(msg);
            deviceInfo.setDeviceState(state);
            deviceInfoService.updateDeviceInfoByCode(deviceInfo);
            //添加到传感器主动上报数据表中
            SensorsReportDataLog sensorsReportDataLog = new SensorsReportDataLog();
            sensorsReportDataLog.setDeviceCode(code);
            sensorsReportDataLog.setDeviceData(msg);
            sensorsReportDataLog.setRoomNum(device.getRoom().getRoomNum());
            sensorsReportDataLogService.insertSensorsReportDataLog(sensorsReportDataLog);
            //code目前不唯一，需要硬件传回主题也就是房间号，后台编号的解析代码可能需要修改
        }else{
            log.error("没有该设备:" + code);
        }

    }





}
